$(document).ready(function(){
    $sLoginlogin='DANG NHAP';
    $sErrorRegisterFill ='Sorry : Please Fill Out Form'; 
    $sErrorRegisterPass ='Plesae RePassWord True';
    $sErrorRegisterEmail ='Error Email ';
    $sErrorRegisterUserID ='UserName Not Avalible ';
    $('#bt_login_cancel').click(function(){
        $('#login_form').dialog("close");
    });
    $('#bt_register_form_cancel').click(function(){
        $('#register_form').dialog("close");
    });
    $('#bt_login').click(function(){
        var sUserName = $('#tb_username_login').val();
        var sPassWord = $('#tb_password_login').val();
        if(sUserName !=''&& sPassWord !='')
        {
            $.post('~user/checkLogin',{username : sUserName, password: sPassWord,status : 1} , function(data){
                if(data.indexOf('FAIL') == -1){
                    document.location.reload(true);

                } 
                else{
                    $('#tb_username_login').val('');
                    $('#tb_password_login').val('');
                    $('#error_id').show();

                }
            });
        }
        else
        {
            $('#error_id').show();
            $('#tb_username_login').val('');
            $('#tb_password_login').val('');
        }
    });

    // Create form dialog for login
    $('#login_form').dialog({
        'autoOpen':false,
        'width':460,
        'height':370,
        'resizable':false,
        'modal':true,
        'dialogClass':'fixed-dialog',
    }).dialog( "widget").addClass('cusstom_form');

    $('#bt_show_login').click(function(){
        $('#login_form').dialog("open");
    });

    $('#bt_dis_login').click(function(){
        $.post('~test/logout',{status:0},function(data){ 
            document.location.reload(true);
        });
    });
    // Ham goi Register Form
    $('#register_form').dialog({
        'autoOpen':false,
        'width':500,
        'height':600,
        'resizable':false,
        'modal':true,
        'dialogClass':'fixed-dialog',
    }).dialog( "widget").addClass('cusstom_form').addClass('cusstom_form_register');
    $('#bt_register').click(function(){
        $('#register_form').dialog("open");
        resetFormRegister();
        $('#img_register').hide();
    });
    // Ham Check User Avaible
    $('#check_user_register').click(function(){
        var sUserName = $('#tb_username_register').val(); 
        if($('#tb_username_register').val() !='')
        {
            $.post('~user/checkUserID',{username : sUserName} , function(data){
                if(data.indexOf('Fail')!=-1)
                {
                    $('#error_register').html($sErrorRegisterUserID);
                    $('#img_register').hide();
                }
                else
                {
                    $('#error_register').html('');
                    $('#img_register').show();
                }
            });
        }
    });
    // Ham kiem tra dang ki
    $('#bt_register_form').click(function(){
        var sUserName = $('#tb_username_register').val();
        var sPassWord = $('#tb_password_register').val();
        var sfullName= $('#tb_fullname_register').val();
        var sAddress = $('#tb_address_register').val();
        var sEmail = $('#tb_email_register').val();
        if(CheckValidateRegister() == true)
        {
            $.post('~user/addUser',{username : sUserName, password: sPassWord, fullname :sfullName, address : sAddress , email : sEmail} , function(data){
                alert(data);
                if(data.indexOf('Success')!=-1)
                {
                    $('#error_register').html('');
                    resetFormRegister();
                    $('#register_form').dialog("close");
                    $.post('~user/checkLogin',{username : sUserName, password: sPassWord, status :1} , function(data){
                        if(data.indexOf('FAIL') != 0){
                            document.location.reload(true);
                        } 
                    });
                }
            });
        }
    });

});
// Ham kiem tra form dang ki
function CheckValidateRegister()
{
    var sUserName = $('#tb_username_register').val();
    var sPassWord = $('#tb_password_register').val();
    var sRePassWord = $('#tb_re_password_register').val();
    var sfullName= $('#tb_fullname_register').val();
    var sEmail= $('#tb_email_register').val();
    var sAddress = $('#tb_address_register').val();
    if(sUserName == ''||sPassWord == ''||sRePassWord ==''||sfullName == ''||sEmail == ''||sAddress == '')
    {
        $('#error_register').html($sErrorRegisterFill);
        resetFormRegister();
        return false;
    }
    if(sPassWord != sRePassWord )
    {
        $('#error_register').html($sErrorRegisterPass);
        resetFormRegister();
        return false;
    }
    if(validateEmail(sEmail) == false)
    {
        $('#error_register').html($sErrorRegisterEmail);
        resetFormRegister();
        return false;
    }
    return true;
}
// Ham kiem tra user_id
function CheckUserID(user_id)
{
    return true;
}

// Ham kiem tra tinh hop le cua email
function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
// Ham kiem tra tinh hop le cua tai khoan
function validateUsername(username) { 
    var re = /^[A-Za-z0-9_]{3,20}$/;
    return re.test(username);
}
// Ham Reset form resgister
function resetFormRegister()
{
    $('#tb_username_register').val('');
    $('#tb_password_register').val('');
    $('#tb_re_password_register').val('');
    $('#tb_fullname_register').val('');
    $('#tb_email_register').val('');
    $('#tb_address_register').val('');
    $('#img_register').hide();
}
// Ham test Login va Register
function checkUsername(){
    $.post('~user/checkUsername',{} , function(data){
        if(data.indexOf('Fail')!=-1)
        {
            $('#bt_show_login').show();
            $('#bt_register').show();
            $('#bt_dis_login').hide();
        }
        else
        {
            $('#bt_show_login').html(data);
            $('#bt_register').hide();
            $('#bt_dis_login').show();
        }
    });
}
function closeBrowser()
{
    $.post('~user/closeBrowser',{} , function(data){

    });
}